<?php
class Third_DomAction{
	
	public $loaded = false;
	public $caminho;
	
	function setCaminho($caminho){
		$this->caminho = $caminho;
	}
	 
	function __construct( $file = null){
		$xml_file = realpath( $file );

		if( $file != null){
			if(@$this->xml = domxml_open_file( $xml_file ) ) {
				$this->loaded = true;
			} else echo "<b>Error:</b> File not found. Ref: ". $file ;
		}	
	}
	
	function getCampos($attr = "value"){
		
		if($this->loaded){
		
		        $ctx = $this->xml->xpath_new_context();
		        if ($xpathObj = @$ctx->xpath_eval($this->caminho)) {
		        
		            foreach($xpathObj->nodeset as $result) {
		            
		                $content = $result->children();
		                
		                foreach($content as $node){
		                	$name = $node->node_name();
		                	if(substr($name,0,1) != "#"){
		                		$campos[$name] =  $this->getAtributos( $node->attributes(), $attr );
		                	}
		                }
		            }
		
		        } else {  return("<b>Error :</b> Wrong XPath query");  }

			return $campos;
		} 
	}
	
	function getAtributos($node, $valor){
		if(is_array($node)){
			foreach($node as $key=>$value){
				if($value->name == $valor)
					return $value->value;
			}
		}
	}	
}
?>